package com.xscd.stonepuzzle;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class MainFrame extends JFrame implements KeyListener {

    int[][] data = {
            {1,2,3,4},
            {5,6,7,8},
            {9,10,11,12},
            {13,14,15,0}
    };

    int[][] win = {
            {1,2,3,4},
            {5,6,7,8},
            {9,10,11,12},
            {13,14,15,0}
    };

    int row; // 0号元素横坐标位置
    int column; // 0号元素列坐标
    int count; //统计步数

    public MainFrame(){

        // 窗体对象.addKeyListener(实现类对象)；
        // this： 当前类对象
        // 窗体对象
        // .addKeyListener实现类对象
        this.addKeyListener(this);
        initFrame();
        initData();
        paintView();

        //设置窗体可见
        setVisible(true);
    }

    /*初始化数据(打乱二维数组)
    * */
    public void initData(){
        //准备random
        Random r = new Random();
        // 遍历数组，获取每一个元素
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                int randomX = r.nextInt(4);
                int randomY = r.nextInt(4);
                //data[i][j]
                //data[randomX][randomY]

                //先将 data[i][j] 的值存储在 temp 中
                int temp = data[i][j];

                //然后将 data[randomX][randomY] 的值赋给 data[i][j]
                data[i][j] = data[randomX][randomY];

                //最后将 temp 的值赋给 data[randomX][randomY]
                data[randomX][randomY] = temp;
            }

        }
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (data[i][j] == 0) {
                    row = i;
                    column = j;
                }
            }
        }
    }
    /*
     * 初始化窗体
     * */
    public void initFrame() {

//        JFrame frame = new JFrame();
        // 设置窗体大小
        setSize(514,595);

        //修改窗体的关闭模式
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // 取消窗体的默认布局,需要指定组件的摆放位置
        setLayout(null);

        // 设置窗体的标题
        setTitle("Xscd-stonepuzzle");

        // 设置窗体的置顶
        setAlwaysOnTop(true);

        // 居中
        setLocationRelativeTo(null);




    }


    /*绘制游戏界面*/
    public void paintView(){

        //加载图片资源之前，需要将现有的组件移除
        super.getContentPane().removeAll();

        if (victory()) {
            //加载胜利图片资源
            JLabel winLabel = new JLabel(new ImageIcon("E:\\Javaimage\\win.png"));
            winLabel.setBounds(124,230,266,88);
            getContentPane().add(winLabel);

        }

        JLabel scoreLabel = new JLabel("步数为：" + count);
        scoreLabel.setBounds(50,20,100,20);
        getContentPane().add(scoreLabel);

        // 重新游戏
        JButton btn = new JButton("再来一局");
        btn.setBounds(350,20,100,20);
        getContentPane().add(btn);
        btn.setFocusable(false);
        btn.addActionListener(e -> {
            count = 0;
            initData();
            paintView();
        });

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                JLabel imgeLabel = new JLabel(new ImageIcon("E:\\Javaimage\\"+data[i][j]+".png"));
                imgeLabel.setBounds(50 + 100 * j,90 + 100 * i,100,100);
                super.getContentPane().add(imgeLabel);
            }

        }

        JLabel bgc = new JLabel(new ImageIcon("E:\\Javaimage\\background.png"));
        bgc.setBounds(26,30,450,484);
        getContentPane().add(bgc);

        // 加载后，需要刷新界面
        super.getContentPane().repaint();
    }

    /*判断是否赢了*/
    public boolean victory(){

        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (data[i][j] != win[i][j]) {
                    return false;
                }
            }
        }
        return true;
    };



    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getExtendedKeyCode();
        move(keyCode);
        // 每一次移动，刷新界面
        paintView();

    }

    /**
     * 此方法用于处理移动业务
     * @param keyCode
     */
    private void move(int keyCode) {

        if (victory()){
            return;
        }

        if (keyCode == 37) {

            if (column == 3) {
                return;
            }
           // 空白块和右侧数据交换
            //data[row][colum] data[row][colum+1]
            int temp = data[row][column];
            data[row][column] = data[row][column + 1];
            data[row][column + 1] = temp;
            column++;
            count++;
        } else if (keyCode == 38) {

            if (row == 3) {
                return;
            }

            int temp = data[row][column];
            data[row][column] = data[row +1][column];
            data[row + 1][column] = temp;
            row++;
            count++;
        } else if (keyCode == 39) {

            if (column == 0) {
                return;
            }

            int temp = data[row][column];
            data[row][column] = data[row][column - 1];
            data[row][column - 1] = temp;
            column--;
            count++;
        }else if (keyCode == 40) {

            if (row == 0) {
                return;
            }

            int temp = data[row][column];
            data[row][column] = data[row -1][column];
            data[row - 1][column] = temp;
            row--;
            count++;
        } else if (keyCode == 90) {
            // 触发作弊器
            data = new int[][]{
                    {1,2,3,4},
                    {5,6,7,8},
                    {9,10,11,12},
                    {13,14,15,0}
            };
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
    @Override
    public void keyTyped(KeyEvent e) {

    }
}
